package org.example.service.serviceimpl;

import lombok.extern.slf4j.Slf4j;
import org.example.entity.University;
import org.example.mapper.UniversityMapper;

import org.example.service.UniversityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
@Slf4j
public class UniversityServiceImpl implements UniversityService {
    @Autowired
    private UniversityMapper universityMapper;

    @Override
    public List<University> getAllUniversity() {
        return universityMapper.getAllUniversity();
    }

    /**
     * 查询数据库里有没有该大学，如果有返回该大学id，如果没有将该大学插入数据库，并返回id
     */
    @Override
    public Integer getUniversityId(String name) {
        //查询数据库里有没有该大学
        University university = universityMapper.getUniversityName(name);
        if (university != null) {
            return university.getSchoolId();
        }
        if (universityMapper.insertUniversity(name)) {
            //查询刚插入的数据
            University university1 = universityMapper.getUniversityName(name);
            return university1.getSchoolId();
        }
        return null;

    }
}
